<template>
  <div class="container">
    <div v-if="showCont && !showTimeOut">
      <div class="headCar">
        <div class="cardCont">
          <div class="vipHead">
            <div class="vipLeft">
              <!-- <img src="/img/vip.png" /> -->
              <div class="iconfont icon-f-vip"></div>
              <div>开发者会员</div>
            </div>
            <div class="vipRight">DEVELOPER MEMBER</div>
          </div>
          <div class="userMain">
            <div class="userImg">
              <img :src="userInfo.head_img" />
            </div>
            <div class="userInfo">
              <div class="userName">{{ userInfo.nick_name }}</div>
              <div class="userTime">
                {{ getTime() }}
              </div>
            </div>
            <div
              class="openBtn"
              v-if="userInfo.is_member != 2"
              @click="showCate = true"
            >
              {{ userInfo.is_member == 1 ? "开通会员" : "延续会员" }}
            </div>
          </div>
        </div>
      </div>
      <div class="vipMain">
        <div class="mainHead">
          <div class="mainItem">
            <div class="headName blodText">开发者会员</div>
            <div class="headVal">
              <div class="priceCont">
                <div class="oldPrice">原价 ¥299/月</div>
                <div class="nowPrice">
                  <span class="bigText">¥149</span>
                  <span class="unitText">/月</span>
                </div>
              </div>
            </div>
          </div>
          <div class="mainItem hasTop">
            <div class="headName">会员数据</div>
            <div class="headVal">优先派单：0 申请接单：{{ userInfo.ljjd }}</div>
          </div>
        </div>
        <div class="fenge"></div>
        <div class="mainTitle">
          <div class="shu"></div>
          <div>会员权益</div>
        </div>
        <div class="formCont">
          <div class="formItem" :key="index" v-for="(item, index) in upArr">
            <div class="formName">{{ index + 1 }}. {{ item.name }}</div>
            <div class="iconfont icon-gouxuan1"></div>
          </div>
        </div>
        <div class="fenge"></div>
        <div class="mainTitle">
          <div class="shu"></div>
          <div>会员说明</div>
        </div>
        <div class="formCont">
          <div class="formItem">
            <div class="formName">1. 购买会员按季度付费。</div>
          </div>
          <div class="formItem">
            <div class="formName">
              2. 开通会员即同意<span class="linkText"
                >《码客直通车会员服务条款》</span
              >。
            </div>
          </div>
          <div class="formItem">
            <div class="formName">
              3. 如有任何问题，可咨询<span class="linkText">在线客服</span>。
            </div>
          </div>
        </div>
      </div>
    </div>
    <van-popup v-model:show="showCate" round position="bottom">
      <div class="maskCont">
        <div class="maskHead">购买企业会员</div>
        <div class="orderMain maskUser">
          <div
            @click="package_id = item.package_id"
            :class="
              package_id == item.package_id
                ? 'orderItem activeItem'
                : 'orderItem'
            "
            :key="index"
            v-for="(item, index) in cateArr"
          >
            <div class="leftCont">
              <span class="bigNum">{{ item.package_num }}</span>
              <span>个月</span>
            </div>
            <div class="moneyNum">¥{{ item.package_price }}</div>
          </div>
        </div>
        <div class="sureBidderBtn" @click="payfn">购买套餐</div>
      </div>
    </van-popup>
    <sub-load v-if="showLoad"></sub-load>
    <load v-if="!showCont"></load>
    <getTimeOut v-if="showTimeOut"></getTimeOut>
  </div>
</template>
<script>
import { memberPay, My, memberPayShow } from "@/http/api";

import WeixinJSBridge from "weixin-js-sdk";
import load from "@/components/public/load.vue";
import getTimeOut from "@/components/public/getTimeOut.vue";
import subLoad from "@/components/public/subLoad.vue";
import {
  showSuccessToast,
  showToast,
  showFailToast,
  showConfirmDialog,
  showDialog,
} from "vant";
export default {
  components: { subLoad, load, getTimeOut },
  name: "",
  data() {
    return {
      showTimeOut: false,
      showCont: false,
      showLoad: false,
      showCate: false,

      userInfo: {},
      cateArr: [],
      package_id: 0,
      upArr: [
        { name: "每天可申请5次项目需求接单" },
        { name: "优先派单，需求项目快人一步" },
        { name: "简历置顶，更容易被雇主挖掘" },
        { name: "接单无限制，全平台项目都可接" },
      ],
    };
  },
  props: {},
  setup() {},
  methods: {
    getCate() {
      memberPayShow()
        .then((res) => {
          if (res.data.code == 1) {
            this.package_id = res.data.data[0].package_id;
            this.cateArr = res.data.data;
          } else {
            showToast(res.data.msg);
          }
        })
        .catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
        });
    },
    getTime() {
      if (this.userInfo.is_member == 1) {
        return "暂未开通";
      } else if (this.userInfo.is_member == 2) {
        return `到期时间：${this.userInfo.member_time}`;
      } else if (this.userInfo.is_member == 3) {
        return `会员已到期`;
      }
    },
    payfn() {
      if (!this.showLoad) {
        this.showLoad = true;
        let data = {
          package_id: this.package_id,
        };
        memberPay(data)
          .then((res) => {
            if (res.data.code == 1) {
              this.wxPayFn(res);
              //   showSuccessToast("开通成功");
            } else {
              showToast(res.data.msg);
              this.showCate = false;
            }
            this.showLoad = false;
          })
          .catch(() => {
            this.showLoad = false;
            this.showCate = false;
          });
      }
    },
    wxPayFn(res) {
      WeixinJSBridge.invoke(
        "getBrandWCPayRequest",
        {
          appId: res.data.data.appId, //公众号ID，由商户传入
          timeStamp: res.data.data.timeStamp, //时间戳，自1970年以来的秒数
          nonceStr: res.data.data.nonceStr, //随机串
          package: res.data.data.package,
          signType: res.data.data.signType, //微信签名方式：
          paySign: res.data.data.paySign, //微信签名
        },
        (resData) => {
          if (resData.err_msg == "get_brand_wcpay_request:ok") {
            // 使用以上方式判断前端返回,微信团队郑重提示：
            //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
            showSuccessToast("支付成功");
            this.showCate = false;
            this.getData();
          } else {
            showFailToast("支付失败");
          }
        }
      );
    },
    getData() {
      My()
        .then((res) => {
          if (res.data.code == 1) {
            this.userInfo = res.data.data;
          } else {
            showToast(res.data.msg);
          }
          this.showCont = true;
        })
        .catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
        });
    },
  },
  mounted() {},
  created() {
    this.getData();
    this.getCate();
    // this.showTimeOut = true;
    // this.showCont = true;
  },
};
</script>
<style scoped lang="less">
.container {
  box-sizing: border-box;
  // padding-bottom: 1.25rem;
  background-color: #fff;
  min-height: 100vh;
  .headCar {
    width: 100%;
    background-color: #86a9fb;
    position: relative;
    padding-top: 0.75rem;
    .cardCont {
      width: 92%;
      margin: 0 auto;
      background-color: #d7e1fc;
      padding: 0.75rem 0.75rem 2rem;
      box-sizing: border-box;
      position: relative;
      border-radius: 0.25rem;
      .vipHead {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .vipLeft {
          display: flex;
          align-items: center;
          font-size: 0.8rem;
          font-weight: bold;
          color: #86a9fb;
          // img{
          //     height: 1.1rem;
          //     margin-right: 0.5rem;
          // }
          .iconfont {
            font-size: 1.1rem;
            font-weight: normal;
            margin-right: 0.25rem;
          }
        }
        .vipRight {
          font-size: 0.8rem;
          color: #aec5fa;
        }
      }
      .userMain {
        width: 100%;
        display: flex;
        margin-top: 1.5rem;
        align-items: center;
        .userImg {
          width: 2.5rem;
          background-color: #86a9fb;
          padding: 0.1rem;
          height: 2.5rem;
          box-sizing: border-box;
          overflow: hidden;
          border-radius: 50%;
          img {
            width: 100%;
            border-radius: 50%;
          }
        }
        .userInfo {
          margin-left: 0.75rem;
          flex: 1;
          width: 1rem;
          .userName {
            font-size: 0.8rem;
            font-weight: bold;
          }
          .userTime {
            color: #666;
            font-size: 0.6rem;
            margin-top: 0.25rem;
          }
        }
        .openBtn {
          padding: 0.45rem 1.25rem;
          background-color: #86a9fb;
          color: #fff;
          font-size: 0.6rem;
          border-radius: 2rem;
        }
      }
    }
  }
  .vipMain {
    background-color: #fff;
    border-radius: 0.75rem 0.75rem 0 0;
    box-sizing: border-box;
    position: relative;
    z-index: 2;
    top: -1rem;
    .mainHead {
      padding: 0.75rem 4% 0;
      box-sizing: border-box;
      .mainItem {
        padding: 0.75rem 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .headName {
          font-size: 0.7rem;
        }
        .blodText {
          font-weight: bold;
          font-size: 0.8rem;
        }
        .headVal {
          margin-left: 0.75rem;
          color: #666;
          .priceCont {
            display: flex;
            align-items: center;
            .oldPrice {
              font-size: 0.6rem;
              color: #999;
              text-decoration: line-through;
            }
            .nowPrice {
              color: #000;
              margin-left: 0.5rem;
              .bigText {
                font-size: 1.5rem;
              }
              .unitText {
                position: relative;
                top: -0.1rem;
              }
            }
          }
        }
      }
      .hasTop {
        border-top: 1px dashed #ccc;
      }
    }
    .fenge {
      width: 100%;
      background-color: #f7f7f7;
      height: 0.5rem;
    }
    .mainTitle {
      padding: 0rem 4%;
      box-sizing: border-box;
      font-size: 0.8rem;
      font-weight: bold;
      margin-top: 0.75rem;
      display: flex;
      align-items: center;
      color: #86a9fb;
      margin-bottom: 0.75rem;
      .shu {
        width: 0.15rem;
        height: 0.8rem;
        border-radius: 0.1rem;
        background-color: #86a9fb;
        margin-right: 0.5rem;
      }
    }
    .formCont {
      width: 100%;
      padding: 0 4%;
      box-sizing: border-box;
      .formItem {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0.5rem;
        .formName {
          color: #333;
          .linkText {
            color: #86a9fb;
            // text-decoration: underline;
          }
        }
        .iconfont {
          color: #86a9fb;
          font-size: 1rem;
        }
      }
    }
  }
  .maskCont {
    padding: 0.75rem 4%;
    box-sizing: border-box;
    // background-color: #f7f7f7;
    .maskHead {
      font-size: 0.8rem;
      font-weight: bold;
      padding: 0.5rem 0;
      text-align: center;
    }
    .maskUser {
      box-sizing: border-box;
      .orderItem {
        // border: 1px solid #fff;
        background-color: #fff;
        border: 1px solid #86a9fb;
        margin-top: 0.75rem;
        padding: 0.75rem;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-radius: 0.25rem;
        .bigNum {
          font-size: 1.5rem;
          margin-right: 0.25rem;
        }
      }
      .activeItem {
        border: 1px solid #86a9fb;
        background-color: #e6edfb;
      }
    }
    .sureBidderBtn {
      width: 100%;
      line-height: 2.2rem;
      font-size: 0.6rem;
      background-color: #86a9fb;
      color: #fff;
      border-radius: 0.25rem;
      text-align: center;
      margin-top: 0.75rem;
    }
  }
}
</style>
